package com.pearl.jacksonspringboot.demo.instantiator;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import org.springframework.beans.BeanUtils;

/**
 * @author TD
 * @version 1.0
 * @date 2024/4/15
 */
public class MyHandlerInstantiator extends HandlerInstantiator {

    @Override
    public JsonSerializer<?> serializerInstance(SerializationConfig serializationConfig, Annotated annotated, Class<?> aClass) {
        // 自定义处理逻辑
        System.out.println("serializerInstance");
        // 返回实例对象
        JsonSerializer<?> jsonSerializer = (JsonSerializer<?>) BeanUtils.instantiateClass(aClass);
        return jsonSerializer;
    }

    @Override
    public JsonDeserializer<?> deserializerInstance(DeserializationConfig deserializationConfig, Annotated annotated, Class<?> aClass) {
        System.out.println("deserializerInstance");
        return null;
    }

    @Override
    public KeyDeserializer keyDeserializerInstance(DeserializationConfig deserializationConfig, Annotated annotated, Class<?> aClass) {
        System.out.println("keyDeserializerInstance");
        return null;
    }


    @Override
    public TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> mapperConfig, Annotated annotated, Class<?> aClass) {
        System.out.println("typeResolverBuilderInstance");
        return null;
    }

    @Override
    public TypeIdResolver typeIdResolverInstance(MapperConfig<?> mapperConfig, Annotated annotated, Class<?> aClass) {
        System.out.println("typeIdResolverInstance");
        return null;
    }
}
